package com.ys.controller.web.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.entity.base.TbPlatform;
import com.ys.service.sys.CommonApiService;
import com.ys.util.AESUtil;
import com.ys.util.DateUtil;

@Controller
@RequestMapping("/web/base/tbPlatform")
public class WebTbPlatformController {
	
	@Autowired
	private CommonApiService<TbPlatform> commonApiService;
	
	/*
	 * 新增
	 */
	@RequiresPermissions("tbPlatform:add")
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
		try {
			TbPlatform entity = new TbPlatform();
			entity.setCode(commonApiService.getCommonNextUkey(MenuId.TB_Platform, "Code"))
				.setStatus(1);
			String aa = commonApiService.toAdd(model, entity, MenuId.TB_Platform, null, null);
            
            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
    			return "base/tbPlatform/toAdd";
			} else {
				return aa;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 修改
	 */
	@RequiresPermissions("tbPlatform:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		TbPlatform entity = new TbPlatform().setId(id).queryFirst();
		Map<String, Object> extraParam = new HashMap<String, Object>();
		extraParam.put("encryptKey", AESUtil.aesDecrypt(entity.getEncryptKey()));
		extraParam.put("encryptSecret", AESUtil.aesDecrypt(entity.getEncryptSecret()));
		extraParam.put("encryptToken", AESUtil.aesDecrypt(entity.getEncryptToken()));
		extraParam.put("validDays", DateUtil.subtractDate(entity.getAuthExpireDate(), entity.getAuthDate()));

		try {
			String aa = commonApiService.toEdit(model, id, new TbPlatform(), MenuId.TB_Platform, extraParam, null);
            
            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
    			return "base/tbPlatform/toEdit";
			} else {
				return aa;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 详情页
	 */
	@RequiresPermissions("tbPlatform:view")
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		TbPlatform entity = new TbPlatform().setId(id).queryFirst();
		Map<String, Object> extraParam = new HashMap<String, Object>();
		extraParam.put("encryptKey", AESUtil.aesDecrypt(entity.getEncryptKey()));
		extraParam.put("encryptSecret", AESUtil.aesDecrypt(entity.getEncryptSecret()));
		extraParam.put("encryptToken", AESUtil.aesDecrypt(entity.getEncryptToken()));
		extraParam.put("validDays", DateUtil.subtractDate(entity.getAuthExpireDate(), entity.getAuthDate()));

		try {
			return commonApiService.toDetail(model, id, new TbPlatform(), MenuId.TB_Platform, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
}
